package com.jackrain.nea.web.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.schema.Table;
import com.jackrain.nea.core.schema.TableManager;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ShiroSessionUtil;
import com.jackrain.nea.web.TableQueryCmd;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.QuerySessionImpl;
import com.jackrain.nea.web.security.Security4Utils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "TableQueryController", description = "获取查询字段rest")
public class TableQueryController {

    @Reference(version = "1",group = "ad")
    private TableQueryCmd tableQueryCmd1;

    @Reference(version = "2",group = "cp")
    private TableQueryCmd tableQueryCmd2;

    @Reference(version = "3",group = "ps")
    private TableQueryCmd tableQueryCmd3;

    @Reference(version = "4",group = "vp")
    private TableQueryCmd tableQueryCmd4;



    @Autowired
    private PropertiesConf pconf;

    private String service = "com.jackrain.nea.web.TableQueryCmd";


    @RequestMapping(path = "/p/cs/getTableQuery",method = {RequestMethod.GET,RequestMethod.POST})
    public JSONObject getTableQuery(HttpServletRequest request,
                                   @RequestParam(value="tableid", required=false)Integer TableId,
                                    @RequestParam(value="getcmd", required=false,defaultValue="n") String getcmd,
                                    @RequestParam(value="table", required=false) String tabname) throws Exception {
        User user = (User) ShiroSessionUtil.getAttribute("user");
        QuerySessionImpl session = new QuerySessionImpl(request);
        TableManager tm = session.getTableManager();

        Table table = null;

        if(TableId!=null) {
            table = tm.getTable(TableId);
        } else {
            table = tm.getTable(tabname);
        }

        if(table==null) {
            throw  new NDSException("table is null");
        }

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);

        JSONObject result = ((TableQueryCmd) o).getTableQuery(user,TableId,getcmd,tabname);
        return result;
    }

    @RequestMapping(path = "/fc/api/getTableQuery",method = {RequestMethod.GET,RequestMethod.POST})
    public String getTableQueryApi(HttpServletRequest request,
                                    @RequestParam(value="tableid", required=false)Integer TableId,
                                    @RequestParam(value="getcmd", required=false,defaultValue="n") String getcmd,
                                    @RequestParam(value="table", required=false) String tabname) throws Exception {
        User user = (User) Security4Utils.getUser(request.getParameter("username"));
        QuerySessionImpl session = new QuerySessionImpl(request);
        TableManager tm = session.getTableManager();

        Table table = null;

        if(TableId!=null) {
            table = tm.getTable(TableId);
        } else {
            table = tm.getTable(tabname);
        }

        if(table==null) {
            throw  new NDSException("table is null");
        }

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);

        JSONObject result = ((TableQueryCmd) o).getTableQuery(user,TableId,getcmd,tabname);
        if(result != null){
            return result.toJSONString();
        }else{
            return "";
        }
    }

}
